using System;
using System.Collections.Generic;
using System.Text;

using libsecondlife;
using libsecondlife.AssetSystem;
using libsecondlife.InventorySystem;

using Feathertail;

namespace NestBox.Inventory
{
    class giveto : Feathertail.Command
    {
        FeathertailClient _Client;

        #region Command Members

        public void Setup(FeathertailClient Client)
        {
            _Client = Client;
        }

        public string HandleInput(string[] CmdLine)
        {


            if (CmdLine.Length != 3)
            {
                return GetHelpText();
            }

            string item2give = CmdLine[1];
            LLUUID target;

            if (LLUUID.TryParse(CmdLine[2], out target) == false)
            {
                return "Invalid target UUID: " + CmdLine[2];
            }

            StringBuilder output = new StringBuilder();

            // Asset for an item in inventory
            string curDirectory = (string)_Client.FeathertailState[Helpers.CurrentDirectoryKey];
            InventoryFolder iFolder = _Client.Inventory.getFolder(curDirectory);

            iFolder.RequestDownloadContents(false, false, true).RequestComplete.WaitOne(15000, false);
            foreach (InventoryBase ib in iFolder.GetContents())
            {
                if (ib is InventoryItem)
                {
                    InventoryItem item = (InventoryItem)ib;
                    if (item.ItemID.Equals(item2give) || item.Name.Equals(item2give))
                    {
                        output.AppendLine("Attempting to give " + item.Name + " [" + item.ItemID + "] to " + target);
                        item.GiveTo(target);
                    }
                }
            }

            return output.ToString();
        }

        public string[] GetCommandStrings()
        {
            return new string[] { "giveto", "give" };
        }

        public string GetSummaryText()
        {
            return "Give an item to someone or something.";
        }

        public string GetHelpText()
        {
            return "Usage: giveto [Item: name | UUID] [Target: UUID]";
        }

        #endregion
    }
}
